#! /usr/bin/env python
# -*- coding: utf8 -*-

# Alexandre Coster <acoster@rhrk.uni-kl.de>
# AG DBIS - Technische Universität Kaiserslautern

# mXML: Messy XML Generator
# This application generates a dirty (messy) XML file (with changes
# both on the values and the structure) based on a "clean" input

import zope.interface

import base
import random
import sys

if __name__ == "__main__":
    print "You shouldn't be running this file!"
    sys.exit()

def changeChars(string, a, b):
    if a > b:
        return changeChars(string, b, a)
    return string[0:a] + string[b] + string[a + 1:b] + string[a] + string[b+1:]

class swapchars(object):
    zope.interface.implements(base.IAlgorithm)
    name = "swapchars"

    def __init__(self, node):
        self.__params = base.node_to_dictionary(node)

        self.__first = 0
        if self.__params["includeFirst"].lower() != "true":
            self.__first = 1

        self.__last = 1
        if self.__params["includeLast"].lower() != "true":
            self.__last = 0

    def __call__(self, string):
        if len(string) <= 1:
            return string

        if len(string) == 2:
            return string[1] + string[0]

        if len(string) == 3 and (self.__last == 1 and self.__first == 1):
            return string

        a = random.randint(0, len(string) - 1)
        b = a

        while b == a:
            b = random.randint(0, len(string) - 1)

        string = changeChars(string, a, b)

        return string

__plugins__ = (swapchars, )
